import Vue from 'vue'
import App from './App'
import * as $apis from './apis/index.js'
import store from './store'
import $mUtils from './common/utils.js'
import $mRouter from './common/router.js'
import $mRoutesConfig from './config/routes.config.js'
import fillter from '@/common/fillter/fillter.js'
import $mConfig from './config/index.config.js'

// #ifdef H5
import Auth from './libs/wechat';
Vue.prototype.$wechat = Auth;
import icanH5api from './common/ican-H5Api.js'
// #endif
import pay from '@/common/pay.js';
// app 基座弹窗
Vue.config.productionTip = false;
// 原型挂载
Vue.prototype.$apis = $apis;
Vue.prototype.$mUtils = $mUtils;
Vue.prototype.$mConfig = $mConfig;
Vue.prototype.$mRouter = $mRouter;
Vue.prototype.$mRoutesConfig = $mRoutesConfig;
Vue.prototype.$store = store;
Vue.prototype.$pay = pay;
// 注入全局混入
import uView from 'uview-ui';
import mixinStore from './store/$u.mixin.js'
Vue.use(uView);
Vue.mixin(mixinStore)
let timer = null;
/* ------注入全局过滤器------ */
Object.keys(fillter).forEach(key => {
    Vue.filter(key, fillter[key])
})
// 注入全局组件
import mLoading from './components/m-loading/m-loading.vue';
import mTabbar from './components/m-tabbar/m-tabbar.vue';
Vue.component('mLoading',mLoading);
Vue.component('mTabbar',mTabbar);
let jRouter = (data) => {
    //路由扩展方便调用
    //path路由对象
    //query携带参数
    //replace跳转方式默认push
    //使用示例jRouter({path:'login',query:{id:1},replace:'redirectTo'})
    if (data.url) {
		let route = $mRoutesConfig[$mUtils.jxUrlName(data.url.split('?')[0])];
		let query = $mUtils.getRequestParameters(data.url);
		switch (data.replace) {
		    case 'redirectTo':
		        $mRouter.redirectTo({
		            route,
		            query
		        })
		        break;
		    case 'reLaunch':
		        $mRouter.reLaunch({
		            route,
		            query
		        })
		        break;
		    case 'switchTab':
		        $mRouter.switchTab({
		            route,
		            query
		        })
		        break;
		    default:
		        $mRouter.push({
		            route,
		            query
		        })
		        // break;
		}
        return
    }
    switch (data.replace) {
        case 'redirectTo':
            $mRouter.redirectTo({
                route: $mRoutesConfig[data.path],
                query: data.query,
            })
            break;
        case 'reLaunch':
            $mRouter.reLaunch({
                route: $mRoutesConfig[data.path],
                query: data.query
            })
            break;
        case 'switchTab':
            $mRouter.switchTab({
                route: $mRoutesConfig[data.path],
                query: data.query
            })
            break;
        default:
            $mRouter.push({
                route: $mRoutesConfig[data.path],
                query: data.query
            })
            // break;
    }
}
Vue.prototype.jRouter = jRouter;


$mRouter.beforeEach((navType, to) => {
	// 自定义路由跳转钩子
	if($mConfig.debug){
		console.log(navType,to)
	}
	const pages = getCurrentPages() || [];
	const page = pages[pages.length - 1];
	if(pages.length && page.$page && page.$page.fullPath == to.route.path){
		console.log('跳转路由重复')
		return 
	}
	
    if(timer){
        clearInterval(timer)
    }
	if(to.route && to.route.isLogin){
		if(!store.getters.hasLogin){
		    store.dispatch('reLogin')
			return
		}
	}
    if (to.route === undefined){
		uni.showToast({
			title: '功能正在开发,敬请期待',
			icon: 'none'
		})
		throw ("路由钩子函数中没有找到to.route对象，路由信息:" + JSON.stringify(to));
	}
	try {
	    uni.setStorageSync('redirect', $mUtils.rouName(to.route.path));
	} catch (e) {
	    // error
	}
    uni[navType]({
        url: $mUtils.objParseUrlAndParam(to.route.path, to.query)
    })
})

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()
